package com.ztgf.order.api.client;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.common.utils.StrUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;
import com.ztgf.order.models.service.IOrderContractParamRelationService;
import com.ztgf.order.models.bo.OrderContractParamRelationBO;
import com.ztgf.order.models.dto.OrderContractParamRelationDTO;

import java.util.ArrayList;
import java.util.List;

/**
 * 订单合同的参数关系表控制器
 *
 * @author YUE
 * @Date 2020-07-20 17:48:20
 */

@RestController
@RequestMapping("/api/orderContractParamRelation")
@Slf4j
@Api(value = "OrderContractParamRelationApiController",tags = "订单合同的参数关系表Api接口")
public class OrderContractParamRelationApiController {

    @DubboReference
    private IOrderContractParamRelationService orderContractParamRelationService;


    @PostMapping("/getByOrderIds")
    @ApiOperation(value = "根据订单id查找",notes = "根据订单id查找")
    public Result<List<OrderContractParamRelationDTO>> getByOrderIds(String orderIds){
        if(StrUtils.isEmpty(orderIds)){
            return new Result<>(CodeEnum.FAIL_100002 );
        }
        List<OrderContractParamRelationDTO> orderContractParamRelationDTOList = new ArrayList<>();
        String[] split = orderIds.split(",");
        for (String s : split) {
            if(StrUtils.isEmpty(s)){
                continue;
            }
            //如果是赊销支付类型，更新合同为赊销合同
            orderContractParamRelationService.updateDefaultContent(Long.parseLong(s));
            //获取合同内容
            OrderContractParamRelationDTO dto = orderContractParamRelationService.getByOrderId(Long.parseLong(s));
            orderContractParamRelationDTOList.add(dto);
        }
        return new Result<>(orderContractParamRelationDTOList);
    }

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找",notes = "根据id查找")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<OrderContractParamRelationDTO> get(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        OrderContractParamRelationDTO dto = orderContractParamRelationService.selectById(id);

        return new Result<>(dto);
    }

    @ApiOperation(value = "订单合同的参数关系表列表",notes = "订单合同的参数关系表列表")
    @PostMapping("/queryPageList")
    public Result<Page<OrderContractParamRelationDTO>> queryPageList(OrderContractParamRelationBO bo){
        Wrapper<OrderContractParamRelationDTO> wrapper = new EntityWrapper<>();
        if (bo.getStartDate() != null){
            wrapper.ge("create_time", bo.getStartDate());
        }
        if (bo.getEndDate() != null){
            wrapper.le("create_time", bo.getEndDate());

        }
        Page<OrderContractParamRelationDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page = orderContractParamRelationService.selectPage(page, wrapper);

        return new Result<>(page);
    }
}
